//Holds the internal ID of the callback interval for the progress bar
var intervalID;

function goToAnchor(anchor) {
	if (anchor != '' && anchor != "$anchor") {
		window.location.hash = anchor;
	}
}

function sendPrintRequest (formId, event, valueElement) {
	method = "post";
    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.getElementById(formId);
    form.setAttribute("method", method);
    form.setAttribute("action", "");

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "_eventId");
    hiddenField.setAttribute("value", event);
    form.appendChild(hiddenField);
    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "collectionId");
    hiddenField.setAttribute("value", valueElement);
    form.appendChild(hiddenField);
    form.submit();
}
function loading() {
	 //var pb = document.getElementById("loading");
	 //if (pb != null) {
	 //	 pb.innerHTML = '<img src="/swf-qudex-spreadsheet/css/ajax-loader.gif" />';
	 //	 pb.className = "loading-visible";
		 $.blockUI({ message: '<img src="/swf-qudex-collections/css/ajax-loader.gif" />' });
	 //}
}
function disableInput(listIds) {
	var arrayIds = listIds.split(";");
	
	for(var i=0; i < arrayIds.length; i++) {
		alert(arrayIds[i]);
		var input = document.getElementById(arrayIds[i]);
		if (input != null)
			input.disabled = "disabled";
	}
}

function show(str) {
	var mdiv = document.getElementById('divInfo');
	if (mdiv.innerHTML == "") {
		if (str == "collectionSpreadsheet") {
			mdiv.innerHTML = "<h3>More information</h3><p>The Collection Level spreadsheet is used to describe either QuDEx collections or sub-collections.</p>" +
				"<p>A QuDex Collection is a Fedora Object that contains metadata annotations to describe a Qualitative Study and also contains the QuDEx XML document describing all the documents and elements associated with this study. The XML instance is stored as a special data type within the object.</p>";
		} else if (str == "fileSpreadsheet") {
			mdiv.innerHTML = "<h3>More information</h3><p>The File Level spreadsheet is used to describe QuDEx documents associated to a particular Fedora QuDEx Collection (Elements created from a Collection Level Spreadsheet).</p>" +
				"<p>A QuDex File is represented in Fedora as a data Object which contains the following information:" +
				"<ul><li>Metadata describing the document</li><li>A reference to the original qualitative material that this object refers to.</li><li>Associated QuDEx categories, memos, codes.</li></ul></p>";
		}
	} else mdiv.innerHTML = "";
}

/* PROGRESS-BAR MANAGEMENT */
function getDataFromServer() {
  	dwrService.getActualProgress({ callback: getDataFromServerCallBack,
  			errorHandler: function(errorString, exception) { alert(errorString); } });
}
function errorHandler(message, ex) {
    dwr.util.setValue("error", "Cannot connect to server. Initializing retry logic.", {escapeHtml:false});
    //setTimeout(function() { dwr.util.setValue("error", ""); }, 5000);
    alert("Here, in error method");
    clearInterval(intervalID);
}

function getDataFromServerCallBack(dataFromServer) {
	 if ($("#status").is(":visible")) {
		 $("#status").animate( { width: dataFromServer }, 500);
		 if (dataFromServer == "100%") {
			 clearInterval(intervalID);
		 }
	 }
}

function initProgressBar() {
    //dwr.engine.setErrorHandler(errorHandler); // Called when a call and all retry attempts fail
	//dwr.engine.setNotifyServerOnPageUnload(true); // Optional - When the page is unloaded, remove this ScriptSession.	    
    //window.setInterval("getDataFromServer()", 2000);
    //dwr.engine.setActiveReverseAjax(true);
    intervalID = setInterval(function() { getDataFromServer(); }, 1000);
}

function setUpFrame() {
	// Activate iframe
	hideFrame(false, "frProgress");
	$('#content').block({ message: null });
}

function popitup(url) {
	newwindow = window.open(url, 'name', 'height=400,width=450');
	if (window.focus) {
		newwindow.focus();
	}
	return false;
}

function pause(millis) {
	var date = new Date();
	var curDate = null;

	do {
		curDate = new Date();
	} while (curDate - date < millis);
}
function hideFrame(hide, frameId) {
	fr = document.getElementById(frameId);
	if (fr != null) {
		if (hide) {
			fr.style.display = "none";
		} else {
			fr.style.display = "block";
		}
	}
}
function clearTimer() {
	clearInterval(intervalID);
}
